package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.AlphabetCoding;
import data.CleaningStatus;
import data.ReservationStatus;

public class AlphabetCodingLogic extends AbstractBL{
	public List<AlphabetCoding> GenerateAlphabetCoding(long hotelId)
	{
		List<AlphabetCoding> list = new ArrayList<AlphabetCoding>();
		
		AlphabetCoding alphabet;
		if(GetAlphabet(hotelId,"Travel Agent Booking") == null){
			alphabet = new AlphabetCoding();
			alphabet.setHotelId(hotelId);
			alphabet.setTitle("Travel Agent Booking");
			alphabet.setAlphabet("T");
			list.add(alphabet);
		}
		if(GetAlphabet(hotelId,"Corporate Booking") == null){
			alphabet = new AlphabetCoding();
			alphabet.setHotelId(hotelId);
			alphabet.setTitle("Corporate Booking");
			alphabet.setAlphabet("C");
			list.add(alphabet);
		}
		if(GetAlphabet(hotelId,"Online/Web Booking") == null){
			alphabet = new AlphabetCoding();
			alphabet.setHotelId(hotelId);
			alphabet.setTitle("Online/Web Booking");
			alphabet.setAlphabet("O");
			list.add(alphabet);
		}
		if(GetAlphabet(hotelId,"Group Booking") == null){
			alphabet = new AlphabetCoding();
			alphabet.setHotelId(hotelId);
			alphabet.setTitle("Group Booking");
			alphabet.setAlphabet("G");
			list.add(alphabet);
		}
		if(GetAlphabet(hotelId,"Other/GDS Booking") == null){
			alphabet = new AlphabetCoding();
			alphabet.setHotelId(hotelId);
			alphabet.setTitle("Other/GDS Booking");
			alphabet.setAlphabet("O");
			list.add(alphabet);
		}
		
		return list;
	}
	
	public List<AlphabetCoding> GetAllAlphabetCoding(long hotelId) {
		Query q = new Query(EntityName.AlphabetCoding);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		q.setFilter(filterHotel);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<AlphabetCoding> GetList(List<Entity> entities) {
		List<AlphabetCoding> list = new ArrayList<AlphabetCoding>();
		if(entities!=null)
		for (Entity e : entities) {
			AlphabetCoding a = new AlphabetCoding();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public AlphabetCoding GetAlphabet(long hotelId,String title)
	{
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);
		Filter filters = CompositeFilterOperator.and(filterHotel, filterTitle);

		Query q = new Query(EntityName.AlphabetCoding).setFilter(filters);
		PreparedQuery pq = datastore.prepare(q);
		try{
			Entity e = pq.asSingleEntity();
			AlphabetCoding c = new AlphabetCoding();
			c.setEntity(e);
			return c;
		}
		catch(Exception e) { return null; }
	}
	
	public AlphabetCoding EditAlphabetCoding(long id,String alphabet) throws EntityNotFoundException{
		AlphabetCoding t = GetAlphabetCoding(id);
		t.setAlphabet(alphabet);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public AlphabetCoding GetAlphabetCoding(long id)
	{
		Key key = KeyFactory.createKey(EntityName.AlphabetCoding, id);
		try {
			Entity lang = datastore.get(key);
			AlphabetCoding c = new AlphabetCoding();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public CleaningStatus GetCleaningStatus(long id)
	{
		Key key = KeyFactory.createKey(EntityName.CleaningStatus, id);
		try {
			Entity lang = datastore.get(key);
			CleaningStatus c = new CleaningStatus();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public CleaningStatus EditCleaningStatusColor(long id,String color) throws EntityNotFoundException{
		CleaningStatus t = GetCleaningStatus(id);
		t.setColor(color);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public ReservationStatus GetReservationStatus(long id)
	{
		Key key = KeyFactory.createKey(EntityName.ReservationStatus, id);
		try {
			Entity lang = datastore.get(key);
			ReservationStatus c = new ReservationStatus();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public ReservationStatus EditReservationStatusColor(long id,String color) throws EntityNotFoundException{
		ReservationStatus t = GetReservationStatus(id);
		t.setColor(color);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
}
